package com.wanmait.schedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wanmait.schedule.entity.Permission;
import com.wanmait.schedule.entity.Role;
import com.wanmait.schedule.entity.RolePermission;
import com.wanmait.schedule.mapper.PermissionMapper;
import com.wanmait.schedule.mapper.RoleMapper;
import com.wanmait.schedule.mapper.RolePermissionMapper;
import com.wanmait.schedule.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2022-12-05
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<Permission> findRoles(List<Integer> idList) {
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(RolePermission::getRoleId,idList);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(queryWrapper);

        if(rolePermissions.isEmpty()){
            return Collections.emptyList();
        }
        List<Integer> permissionsIds = rolePermissions.stream()
                .map(rolePermission -> rolePermission.getPermissionId())
                .collect(Collectors.toList());
        return permissionMapper.selectBatchIds(permissionsIds);
    }
}
